package com.demo.controller;

import com.demo.model.User;
import com.demo.service.UserService;
import com.demo.vo.UserParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sdyang
 * @create 2019-05-01 14:30
 **/
@Api(value = "用户接口", tags = "用户接口")
@RestController
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    @GetMapping("findAll")
    public List<User> findAll() {
        return userService.findAll();
    }

    @ApiOperation(value = "保存用户信息", notes = "新增或修改")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User", paramType = "body")
    @PostMapping("save")
    public String save(@RequestBody User user) {
        try {
            userService.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
        return "success";
    }

    @ApiOperation(value = "根据id删除用户", notes = "根据id删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "query")
    @GetMapping("delete")
    private String delete(Long id) {
        try {
            userService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
        return "success";
    }

    @ApiOperation(value = "查询用户信息", notes = "查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginid", value = "登录id", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataType = "String", paramType = "form")
    })
    @GetMapping("find")
    public List<User> find(@RequestParam("loginid") String loginid, @RequestParam("username") String username, @RequestParam("mobile") String mobile) {
        try {
            UserParam param = new UserParam();
            param.setMobile(mobile);
            param.setLoginid(loginid);
            param.setUsername(username);
            return userService.find(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
